import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:mokeng/routes/app_pages.dart';
import 'package:mokeng/utils/loading.dart';
import 'package:mokeng/values/values.dart';

import 'logic.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});

  bool canPop(BuildContext context) {
    if (controller.lastBackPressed == null ||
        DateTime.now().difference(controller.lastBackPressed!) > Duration(seconds: 2)) {
      controller.lastBackPressed = DateTime.now();
      toast('再按一次退出应用');
      // showSnackBar(
      //   SnackBar(content: Text('再按一次退出应用')),
      // );
      return false;
    }
    controller.lastBackPressed = null;
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: canPop(context),
      // onPopInvoked: (didPop) {
      //   // 这里可以做一些额外的动作，例如关闭当前页面
      //   Navigator.pop(context);
      // },
      child: GetRouterOutlet.builder(
        route: Routes.HOME,
        builder: (context) {
          return Scaffold(
            body: GetRouterOutlet(
              initialRoute: Routes.Trade,
              anchorRoute: Routes.HOME,
              filterPages: (pages) { //保存页面状态的关键
                var ret = pages.toList();
                final nav = Get.nestedKey(Routes.HOME)?.navigatorKey.currentState?.widget;
                Get.log('Home filter pages: ${pages.map((e) => e.name)}');

                if (nav != null) {
                  if (ret.isEmpty) {
                    Get.log("Home use olds: ${nav.pages.map((e) => e.name)}");
                    return nav.pages as List<GetPage>;
                  }
                  final sn = ret[0].name.split('/').length;
                  for (var p in nav.pages as List<GetPage>) {
                    if (p.maintainState &&
                        p.name.split('/').length == sn &&
                        !ret.contains(p)) {
                      ret.insert(0, p);
                    }
                  }
                }
                ret = ret
                    .where((e) => e.participatesInRootNavigator != true)
                    .toList();
                Get.log('Home real pages: ${ret.map((e) => e.name)}');
                return ret;
              },
            ),
            bottomNavigationBar: IndexedRouteBuilder(
              routes: const [
                Routes.Trade,
                Routes.Found,
                Routes.Store,
                Routes.Mine
              ],
              builder: (context, routes, index) {
                final delegate = context.delegate;
                return BottomNavigationBar(
                  items: controller.bottomTabs,
                  currentIndex: index,
                  fixedColor: AppColors.primaryText,
                  type: BottomNavigationBarType.fixed,
                  onTap: (value) => delegate.toNamed(routes[value]),
                  selectedFontSize: 24.sp,
                  unselectedFontSize: 24.sp,
                  showSelectedLabels: false,
                  showUnselectedLabels: false,
                );
              },
            ),
          );
        },
      ),
    );
  }
}
